import { getGoodsList } from '../../apis/goods'
const app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        searchStr: '',
        allList: [],
        list1: [],
        list2: [],
        list3: [],
        list4: [],
        list5: []
    },
    async getGoods(categoryId) {
        let params = {
            pageNum: 1,
            pageSize: 1000,
            goodsName: this.data.searchStr || ''
        }
        if (categoryId) {
            params.categoryId = categoryId
        }
        const res = await getGoodsList(params)
        return res.list
    },
    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        if (options.tab) {
            this.setData({
                active: Number(options.tab),
                ['list' + options.tab]: await this.getGoods(options.tab)
            })
        } else {
            this.setData({
                allList: await this.getGoods()
            })
        }
    },
    async onChange(event) {
        const { index } = event.detail
        if (!index) {
            this.setData({
                allList: await this.getGoods()
            })
        } else {
            this.setData({
                ['list' + index]: await this.getGoods(index)
            })
        }
    },
    async onSearch() {
        const { active } = this.data
        if (!active) {
            this.setData({
                allList: await this.getGoods()
            })
        } else {
            this.setData({
                ['list' + active]: await this.getGoods(active)
            })
        }
    },
    onSearchChange(e) {
        this.setData({
            searchStr: e.detail
        })
    },
    gotoDetail(e) {
        const id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/pages/detail/index?id=' + id
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {}
})
